﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    #region Web User Control
    public TextBox WUC_Textbox1
    {
        get
        {
            return this.TextBox1;
        }
    }
    public TextBox WUC_Textbox2
    {
        get
        {
            return this.TextBox2;
        }
    }
    public CheckBox WUC_Checkbox1
    {
        get
        {
            return this.CheckBox1;
        }
    }
    public Button WUC_Button1
    {
        get
        {
            return this.Button1;
        }
    }
    #endregion
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection connection = new SqlConnection())
        {
            connection.ConnectionString = GlobalVar.ConnString;
            connection.Open();
            // Use the connection
            using (SqlCommand command = new SqlCommand())
            {
                command.CommandText = "SELECT User_ID,Username,Password FROM Users";
                command.Connection = connection;
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    StringBuilder sb = new StringBuilder();
                    while (reader.Read())
                    {
                        if (reader.GetString(1) == WUC_Textbox1.Text.ToString())
                        {
                            Session["userID"] = reader.GetInt32(0);
                            Session["userName"] = reader.GetString(1);
                            Response.Redirect("Home.aspx");
                            break;
                        }
                    }
                    if(Session["userID"]==null)
                        Label1.Text = "*invalid username or password";
                }
            }
            connection.Close();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    }
}
